﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;
using dpdw.CompoundPattern;

namespace dpdw.testing
{
    [TestClass]
    public class TestCompoundPattern
    {
        public void Simulate(IQuackable duck)
        {
            duck.Quack();
        }

        [TestMethod]
        public void TestCompoundPatternMethod1()
        {
            Trace.WriteLine("\r\n" + "TestCompoundPatternMethod1");

            IQuackable mallardDuck = new QuackCounter(new MallardDuck());
            IQuackable redheadDuck = new QuackCounter(new RedheadDuck());
            IQuackable duckCall = new QuackCounter(new DuckCall());
            IQuackable rubberDuck = new QuackCounter(new RubberDuck());

            IQuackable gooseAdapter = new GooseAdapter(new Goose());

            Trace.WriteLine("========================================");
            Trace.WriteLine("Duck Simulator: With Decorator.");

            Simulate(mallardDuck);
            Simulate(redheadDuck);
            Simulate(duckCall);
            Simulate(rubberDuck);
            Simulate(gooseAdapter);

            Trace.WriteLine("========================================");
            Trace.WriteLine("The ducks quacked: " 
                + QuackCounter.GetQuacks().ToString() + " times.");
        }

        [TestMethod]
        public void TestCompoundPatternMethod2()
        {
            Trace.WriteLine("\r\n" + "TestCompoundPatternMethod2");

            AbstractDuckFactory duckFactory = new CountingDuckFactory();

            IQuackable mallardDuck = duckFactory.CreateMallardDuck();
            IQuackable redheadDuck = duckFactory.CreateRedheadDuck();
            IQuackable duckCall = duckFactory.CreateDuckCall();
            IQuackable rubberDuck = duckFactory.CreateRubberDuck();

            IQuackable gooseAdapter = new GooseAdapter(new Goose());

            Trace.WriteLine("========================================");
            Trace.WriteLine("Duck Simulator: With Decorator.");

            Simulate(mallardDuck);
            Simulate(redheadDuck);
            Simulate(duckCall);
            Simulate(rubberDuck);
            Simulate(gooseAdapter);

            Trace.WriteLine("========================================");
            Trace.WriteLine("The ducks quacked: "
                + QuackCounter.GetQuacks().ToString() + " times.");
        }

        [TestMethod]
        public void TestCompoundPatternMethod3()
        {
            Trace.WriteLine("\r\n" + "TestCompoundPatternMethod3");

            AbstractDuckFactory duckFactory = new CountingDuckFactory();

            IQuackable redheadDuck = duckFactory.CreateRedheadDuck();
            IQuackable duckCall = duckFactory.CreateDuckCall();
            IQuackable rubberDuck = duckFactory.CreateRubberDuck();

            IQuackable gooseAdapter = new GooseAdapter(new Goose());

            Trace.WriteLine("========================================");
            Trace.WriteLine("Duck Simulator: With Composite - Flocks.");

            Flock flockOfDucks = new Flock();

            flockOfDucks.Add(redheadDuck);
            flockOfDucks.Add(duckCall);
            flockOfDucks.Add(rubberDuck);
            flockOfDucks.Add(gooseAdapter);

            Flock flockOfMallards = new Flock();

            IQuackable mallardDuckOne = duckFactory.CreateMallardDuck();
            IQuackable mallardDuckTwo = duckFactory.CreateMallardDuck();
            IQuackable mallardDuckThree = duckFactory.CreateMallardDuck();
            IQuackable mallardDuckFour = duckFactory.CreateMallardDuck();

            flockOfMallards.Add(mallardDuckOne);
            flockOfMallards.Add(mallardDuckTwo);
            flockOfMallards.Add(mallardDuckThree);
            flockOfMallards.Add(mallardDuckFour);

            flockOfDucks.Add(flockOfMallards);

            Trace.WriteLine("========================================");
            Trace.WriteLine("Duck Simulator: Whole Flock Simulation.");

            Simulate(flockOfDucks);

            Trace.WriteLine("========================================");
            Trace.WriteLine("The ducks quacked: "
                + QuackCounter.GetQuacks().ToString() + " times.");
        }

        [TestMethod]
        public void TestCompoundPatternMethod4()
        {
            Trace.WriteLine("\r\n" + "TestCompoundPatternMethod4");

            AbstractDuckFactory duckFactory = new CountingDuckFactory();

            MallardDuck mallardDuck = new MallardDuck();

            Trace.WriteLine("========================================");
            Trace.WriteLine("Duck Simulator: With Observer.");

            Quackologist quackologistOne = new Quackologist();
            mallardDuck.RegisterObserver(quackologistOne);

            Quackologist quackologistTwo = new Quackologist();
            mallardDuck.RegisterObserver(quackologistTwo);

            Simulate(mallardDuck);

            Trace.WriteLine("========================================");
            Trace.WriteLine("The ducks quacked: "
                + QuackCounter.GetQuacks().ToString() + " times.");
        }
    }
}
